package com.test;

import com.alibaba.fastjson.JSON;
import com.test.entry.Order;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.joda.time.DateTime;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class RocketMqProducer {

    private static DefaultMQProducer producer;
    private static String[] accountArray = new String[]{"zhangsan", "lisi", "wangwu", "maliu", "liugui", "qiqi", "bagun", "jiusi"};
    private static String[] nameArray = new String[]{"张三", "里斯", "王五", "马流", "流归", "萋萋", "把滚", "就死"};
    private static String[] codeArray = new String[]{"001", "002", "003", "004", "005", "006", "007", "008"};
    private static BigDecimal[] amountArray = new BigDecimal[]{new BigDecimal(10), new BigDecimal(11), new BigDecimal(12), new BigDecimal(13)
            , new BigDecimal(14), new BigDecimal(15), new BigDecimal(16), new BigDecimal(17)};


    public static void main(String[] args) throws Exception {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("test-topic-producer");
        defaultMQProducer.setNamesrvAddr("10.0.0.217:9876");
        defaultMQProducer.setMaxMessageSize(1000);
        defaultMQProducer.setSendMsgTimeout(3000);
        defaultMQProducer.setVipChannelEnabled(false);
        defaultMQProducer.start();

        producer = defaultMQProducer;


        for (int i = 0; i < 1; i++) {
            int min = 0;
            int max = 7;
            int a = (int) (min + Math.random() * (max - min + 1));
            int b = (int) (min + Math.random() * (max - min + 1));
            a=1;

            Order order = new Order();
            order.setAccountNo(accountArray[a]);
            order.setName(nameArray[a]);
            order.setCode(codeArray[b]);
            order.setAmount(amountArray[b]);
            order.setCreateDate(new Date());
         /*   if (i == 7 || i == 3 || i == 5) {
                order.setCreateDate(new DateTime().plusMinutes(1).toDate());
            }*/
           /* if (i != 0) {
            }*/
            order.setCreateDate(new DateTime().plusMinutes(2).toDate());

            System.out.println(JSON.toJSONString(order) + ":" + new DateTime(order.getCreateDate()).toString("yyyy-MM-dd hh:mm:ss"));
            sendRecord(order);
        }
    }

    public static void sendRecord(Order resultList) {

        byte[] body = JSON.toJSONString(resultList).getBytes(Charset.forName("utf-8"));
        Message message = new Message("FLINK-TEST-TOPIC", body);

        try {
            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                }

                @Override
                public void onException(Throwable e) {
                    System.out.println("日志发送失败" + ExceptionUtils.getMessage(e));
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("日志-消息发送异常:" + e.getMessage());
        }
    }
}
